package HTML::Jquery;

use strict;
#use base qw/Class::Accessor::Fast/;
use vars qw/$VERSION $callbacks/;
use HTML::Element;
use HTML::Prototype::Helper;

$VERSION = '0.01';
my $callbacks    = [qw/success error complete/];
my $ajax_options = [qw/url type data dataType ifModified timeout global/];

=head1 NAME

HTML::Prototype - Generate HTML and Javascript for the Prototype library

=head1 SYNOPSIS

    use HTML::Jquery;

    my $jquery = HTML::Jquery->new;
    print $jquery->auto_complete_field(...);
    print $jquery->auto_complete_result(...);
    print $jquery->auto_complete_stylesheet(...);
    print $jquery->content_tag(...);
    print $jquery->define_javascript_functions;
    print $jquery->draggable_element(...);
    print $jquery->drop_receiving_element(...);
    print $jquery->evaluate_remote_response(...);
    print $jquery->form_remote_tag(...);
    print $jquery->in_place_editor(...);
    print $jquery->in_place_editor_field(...);
    print $jquery->in_place_editor_stylesheet(...);
    print $jquery->javascript_tag(...);
    print $jquery->link_to_function(...);
    print $jquery->link_to_remote(...);
    print $jquery->observe_field(...);
    print $jquery->observe_form(...);
    print $jquery->periodically_call_remote(...);
    print $jquery->sortable_element(...);
    print $jquery->submit_to_remote(...);
    print $jquery->tag(...);
    print $jquery->text_field_with_auto_complete(...);
    print $jquery->update_element_function(...);
    print $jquery->visual_effect(...);
=cut

=item $jquery->content_tag( $name, $content, \%html_options )

Returns a block with opening tag, content, and ending tag. Useful for
autogenerating tags like B<<a href="http://catalyst.perl.org">Catalyst
Homepage</a>>. The first parameter is the tag name, i.e. B<'a'> or
B<'img'>.

=cut

sub content_tag {
    my ( $self, $name, $content, $html_options ) = @_;

    return HTML::Prototype::Helper::Tag->_content_tag( $name, $content,
        $html_options );
}

sub _options_for_javascript {
    my $options = shift;
    my @options = ();
    while ( my ( $key, $value ) = each %{$options} ) {
        push @options, "$key:$value";
    }
    return '{ ' . join( ', ', sort(@options) ) . ' }';
}
sub _options_to_js_options {
    my ( $to_options, $options ) = @_;

    $to_options ||= {};
    $options    ||= {};

    my $js_options = {};
    while ( my ( $key, $js_key ) = each %{$to_options} ) {
        if ( $options->{$key} ) {
            if ( ref $js_key eq 'SCALAR' ) {
                $js_options->{ ${$js_key} } = "'" . $options->{$key} . "'";
            }
            else {
                $js_options->{$js_key} = $options->{$key};
            }
        }
    }

    return $js_options;
}

sub _remote_function {
	#here we should build the remote functions with $.ajax or $.load or $.get or $.post?
    my $options    = shift;
    my $js_options = _options_for_ajax($options);
    my $update     = $options->{update};
    my $url = $options->{url} || '';
    $function .= " '$url', $js_options ) ";
    
	$function = "$.ajax(".options_for_ajax($options).")"
    return $function;
}

sub _options_for_ajax
{
	my $options = shift;
	$js_options = _build_callbacks( $options );

 	$options->{type} ||= "'POST'";
    $js_options->{data} = $options->{data} if $options->{data};

	return _options_for_javascript( $js_options );   

}
sub _build_callbacks
{
 	my $options = shift;
    my %callbacks;
    for my $callback (@$callbacks) {
        if ( my $code = $options->{$callback} ) {
            my $name =  dc $callback;
            $callbacks{$name} = "function(request){$code}";
        }
    }
    return \%callbacks;

}
